<template>
  <div class="center_box">
    <el-card class="box-card">
      <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" class="demo-ruleForm">
        <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            action="http://localhost:80/user/upload"
            :on-success="handleAvatarSuccess">
          <img v-if="formData.avatar" :src="formData.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="formData.nickname"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="formData.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-select v-model="formData.age" placeholder="请选择年龄">
            <el-option :label="num" :value="num" v-for="num in 100"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('formData')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "UserCenter",
  data() {
    return {
      formData: {
        id: '',
        avatar: '',
        nickname: '',
        age: '',
        sex: '',
      },
      rules: {
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
        ],
        age: [
          { required: true, message: '请选择年龄', trigger: 'change' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getFormData() {

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否确认修改？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$axios.post('/user/update', this.formData).then(res => {
              //修改了表单数据，重新查询显示 - 重新加载页面
              location.reload()
            })
            this.$message({
              type: 'success',
              message: '保存成功!'
            });
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '取消成功'
            });
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleAvatarSuccess(res) {
      this.formData.avatar = res.data
    },
    getUserInfo() {
      //从sessionStorage中获取用户信息
      this.$axios.get("/user/getUser/" + sessionStorage.getItem("userId")).then(res => {
        //将userId保存到表单对象中，一起发送表单 - 回显表单
        this.formData = res.data.data
        // this.formData.id = res.data.data.id
        // this.formData.avatar = res.data.data.avatar
      })
    }
  }
}
</script>

<style scoped>
.el-button {
  margin-left: 60px;
}

.el-form {
  margin-right: 50px;
}

.box-card {
  margin-bottom: 250px;
  width: 480px;
  display: flex;
  justify-content: center;
}

img {
  border-radius: 50%;
  border: #5be8a9 1px solid;
  margin-left: 130px;
  margin-bottom: 15px;
}

.center_box {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.avatar-uploader .el-upload {
  border: 1px dashed rgba(0, 0, 0, 0.8);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 108px;
  display: block;
}

.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}
</style>